#ifndef HEIGHTRECORDMODEL_H
#define HEIGHTRECORDMODEL_H

#include <QAbstractListModel>
#include <QDate>
#include <QVariantList>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "HeightRecord.h"

class HeightRecordModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int count READ count NOTIFY countChanged)
    Q_PROPERTY(double currentHeight READ currentHeight NOTIFY currentHeightChanged)
    Q_PROPERTY(double lastYearGrowth READ lastYearGrowth NOTIFY lastYearGrowthChanged)

public:
    enum HeightRecordRoles {
        DateRole = Qt::UserRole + 1,
        HeightRole,
        DateStringRole,
        YearRole,
        MonthRole,
        DayRole
    };

    explicit HeightRecordModel(QObject *parent = nullptr);

    // QAbstractListModel 接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    // 属性访问器
    int count() const { return m_records.size(); }
    double currentHeight() const;
    double lastYearGrowth() const;

    // Q_INVOKABLE 方法
    Q_INVOKABLE void addRecord(const QDate& date, double height);
    Q_INVOKABLE void addRecord(const QString& dateString, double height);
    Q_INVOKABLE void deleteRecord(int index);
    Q_INVOKABLE QString getYearlyGrowthDetails() const;
    Q_INVOKABLE double getGrowthBetweenDates(const QDate& startDate, const QDate& endDate) const;
    Q_INVOKABLE QVariantList getChartData() const;
    
    // 云同步方法（与SkillModel共用配置）
    Q_INVOKABLE void syncFromCloud();  // 从云端下载
    Q_INVOKABLE void syncToCloud();    // 上传到云端
    
    // 云同步配置（从SkillModel获取）
    void setCloudSyncConfig(const QString& owner, const QString& repo, const QString& token);
    bool isCloudSyncConfigured() const;
    
    // 智能云同步检查
    Q_INVOKABLE void checkAndSyncOnStartup();  // 启动时检查云端数据
    Q_INVOKABLE void checkAndSyncOnExit();     // 退出时检查本地数据
    
    // 数据修改状态
    Q_INVOKABLE bool hasDataModified() const;  // 检查数据是否已修改
    Q_INVOKABLE void clearModifiedFlag();      // 清除修改标志

signals:
    void countChanged();
    void currentHeightChanged();
    void lastYearGrowthChanged();
    
    // 云同步信号
    void syncStarted();
    void syncFinished();
    void syncFailed(const QString& error);
    
    // 智能同步信号
    void cloudDataDifferent(const QString& cloudHash, const QString& localHash);  // 云端数据不同
    void localDataDifferent(const QString& localHash, const QString& cloudHash);  // 本地数据不同
    void dataConsistent();  // 数据一致

private:
    QList<HeightRecord> m_records;
    
    // 云同步相关
    QNetworkAccessManager* m_networkManager;
    QString m_giteeOwner;
    QString m_giteeRepo;
    QString m_giteeToken;
    QString m_currentFileSha;  // 用于更新文件

    void loadData();
    void saveData();
    void sortRecords();  // 按日期排序
    
    // 云同步辅助方法
    QString getCloudApiUrl() const;
    QString calculateDataHash() const;  // 计算数据哈希
    
    // 数据变更追踪
    bool m_dataModified;  // 标记数据是否被修改
};

#endif // HEIGHTRECORDMODEL_H

